﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Navigation_Manage : System.Web.UI.Page
{

    public int ParentID
    {
        get { return Convert.ToInt32(ViewState["_ParentID"]); }
        set { ViewState["_ParentID"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ParentID = DongDong.Common.Misc.GetQueryStringAsInt("ParentID");
            PopulateNavagation();
        }
    }

    public void PopulateNavagation()
    {
        lvNavagation.DataSource = Navigation.GetAllByParentID(ParentID, "");
        lvNavagation.DataBind();
    }

    protected void lvNavagation_ItemCanceling(object sender, ListViewCancelEventArgs e)
    {
        lvNavagation.EditIndex = -1;
        btnSerach_Click(null, null);

    }

    protected void lvNavagation_ItemEditing(object sender, ListViewEditEventArgs e)
    {
        lvNavagation.EditIndex = e.NewEditIndex;
        btnSerach_Click(null, null);
        int ID = DongDong.Common.Misc.ConvertToInt(lvNavagation.DataKeys[e.NewEditIndex].Value);
        Navigation objNavigation = Navigation.Get(ID);
        DropDownList drpParent = (DropDownList)lvNavagation.Items[e.NewEditIndex].FindControl("drpParent");
        Navigation objPare = Navigation.Get(objNavigation.ParentID);
        if (objPare != null)
        {
            drpParent.DataSource = Navigation.GetAll().Where(row => row.ParentID == objPare.ParentID);
            drpParent.DataTextField = "Title";
            drpParent.DataValueField = "ID";
            drpParent.DataBind();
            drpParent.SelectedValue = objNavigation.ParentID.ToString();
        }
        else
        {
            drpParent.Items.Insert(0, new ListItem("请选择", "0"));
        }
    }
    protected void lvNavagation_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {
        int ID = DongDong.Common.Misc.ConvertToInt(lvNavagation.DataKeys[e.ItemIndex].Value);
        Navigation.Delete(ID);
        btnSerach_Click(null, null);
    }

    protected void lvNavagation_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        int ID = DongDong.Common.Misc.ConvertToInt(lvNavagation.DataKeys[e.ItemIndex].Value);
        Navigation objNagation = Navigation.Get(ID);
        var item = lvNavagation.Items[e.ItemIndex];

        TextBox txtEdiTitle = (TextBox)item.FindControl("txtEdiTitle");
        TextBox txtEidtURL = (TextBox)item.FindControl("txtEidtURL");
        CheckBox cbEditKey = (CheckBox)item.FindControl("cbEditKey");
        FileUpload fulEditImg = (FileUpload)item.FindControl("fulEditImg");
        objNagation.Title = txtEdiTitle.Text;
        objNagation.URL = txtEidtURL.Text;
        objNagation.Update();
        lvNavagation.EditIndex = -1;
        btnSerach_Click(null, null);
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Navigation objNavigation = new Navigation();
        objNavigation.Title = txtAddName.Text;
        objNavigation.RootID = ParentID;
        objNavigation.Rank = DongDong.Common.Misc.ConvertToDateTime("2000-12-12");
        if (ParentID != 0)
        {
            Navigation objNava = Navigation.Get(ParentID);
            if (objNava.ParentID != 0)
                objNavigation.RootID = objNava.RootID;
        }
        objNavigation.ParentID = ParentID;
        objNavigation.URL = txtAddURL.Text;
        objNavigation.Add();
        btnSerach_Click(null, null);
    }

    protected void btnSerach_Click(object sender, EventArgs e)
    {
        string key = txtSearchKey.Text;

        lvNavagation.DataSource = Navigation.GetAllByParentID(ParentID, key);
        lvNavagation.DataBind();


    }


    protected void lvNavagation_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        int ID = DongDong.Common.Misc.ConvertToInt(e.CommandArgument);
        if (e.CommandName == "Return")
        {
            Navigation objParentNavigation = Navigation.Get(ID);
            Response.Redirect("Manage.aspx?ParentID=" + (objParentNavigation == null ? "0" : objParentNavigation.ParentID.ToString()));

        }
        if (e.CommandName == "Up")
        {
            Navigation objParentNavigation = Navigation.Get(ID);
            objParentNavigation.Rank = System.DateTime.Now;
            objParentNavigation.Update();
            btnSerach_Click(null, null);
        }

    }

    public string NavagationTitleByID(object ID)
    {
        string Title = "没有父导航";
        Navigation objNavagition = Navigation.Get(DongDong.Common.Misc.ConvertToInt(ID));
        if (objNavagition != null)
            Title = objNavagition.Title;
        return Title;
    }

    protected void lbtnReturn_Click(object sender, EventArgs e)
    {
        if (ParentID != 0)
        {
            Navigation objNa = Navigation.Get(ParentID);
            if (objNa != null)
                Response.Redirect("/Admin/Navigation/Manage.aspx?ParentID=" + objNa.ParentID);

        }
    }
}